﻿var GD = GD || {};
var self = GD.front = {
	init: function(name){
		self.quick_box();
		self.link_select();
		self.notice_change();
		if($(".gallery").length>0){
			self.gallery();
		}
		if($("#left-menu").length>0){
			self.left_menu();
			//set rightContent title
			self.setContentTitle();
		}
		if($("#loginForm, #regForm").length>0){
			$("#loginForm, #regForm").Validform({
				btnSubmit:"#btn_sub",
				tiptype:function(msg,o,cssctl){
					if(o.obj){
						var objtip=o.obj.parent().next();
						cssctl(objtip,o.type);
						objtip.text(msg);
					}
				},
				tipSweep:false,
				postonce:true
			});
		}

	},
	quick_box: function(){
		/*提交搜索*/
		$("#quickbox .search-submit").click(function(){
			if($("input[name='s']").val().trim()!=""){
				$("#search").submit();
			}else{
				return false;
			}
		});
		/*内部通知*/
		/*
		$("#inner-notice-bt").click(function(){
			$(this).addClass("focus");
			$("#other-bt").removeClass("focus");
			$("#innernotice-content").show();
			$("#other-content").hide();

		});
		$("#other-bt").click(function(){
			$(this).addClass("focus");
			$("#inner-notice-bt").removeClass("focus");
			$("#innernotice-content").hide();
			$("#other-content").show();
		});
		*/
	},
	gallery: function(target){
		var root = $(".gallery");
		var title_wrap = root.find(".pic-title");
		var picselect_wrap = root.find(".picselect-wrap");
		var pic_wrap = root.find("ul");
		var pics = pic_wrap.find("li");
		var pic_num = pics.length;
		var counter = 0;
		var int_handler;
		for(i=0; i<pic_num; i++){
			var obj = $("<div></div>");
			obj.addClass("picselect");
			obj.attr("value",i);
			picselect_wrap.append(obj);
		}

		this.changePic = function (){
			pics.hide();
			$(pics[counter]).show();
			picselect_wrap.find("div").removeClass("hoveron");
			$(picselect_wrap.find("div")[counter]).addClass("hoveron");

			var img_href = $(pics[counter]).find("a");
			var title_obj = $("<a></a>");
			title_obj.text(img_href.attr("title"));
			title_obj.attr("href",img_href.attr("href"));
			title_wrap.children().remove();
			title_wrap.append(title_obj);

			counter = (counter+1)%pic_num;
		}

		picselect_wrap.find("div").hover(function(){
			counter = $(this).attr("value")||0;
			window.clearInterval(int_handler);
			int_handler = setInterval("self.changePic()",2000);
			self.changePic();
		});

		this.changePic();
		picselect_wrap.show();
		int_handler = setInterval("self.changePic()",2000);
	},
	notice_change: function(){
		$("#notice .teacher-title").hover(function(){
			$(this).addClass("hoveron");
			$("#notice .stu-title").removeClass("hoveron");
			$("#teacher-notice").show();
			$("#stu-notice").hide();
			$("#notice .more").attr("href","notice.htm?tg=teacher");
		});
		$("#notice .stu-title").hover(function(){
			$(this).addClass("hoveron");
			$("#notice .teacher-title").removeClass("hoveron");
			$("#stu-notice").show();
			$("#teacher-notice").hide();
			$("#notice .more").attr("href","notice.htm?tg=stu");
		});
	},
	link_select: function(){
		/*相关链接选择*/
		$("select[name='link']").change(function(){
			if($(this).get(0).selectedIndex !=0){
				window.open($(this).val());
			}
		});
	},
	left_menu: function(){
		$("#bodier .menu-list h3").click(function(){
			if($(this).next().length>0){
				$(this).next().slideToggle(200);
			}else{
				$("#bodier .menu-list h3").removeClass("selected");
				$("#bodier .menu-list li li").removeClass("selected");
				$(this).addClass("selected");
				//fix title
				$("#right-content .headtitle").text($(this).text());
				$("#right-content .separator").text("");
				$("#right-content .subtitle").text("");
				//Ajax call
				$("#right-content .ajax_loading").show();
				$.ajax({
					url:"/edu_ajax.do",
					dataType: "html",
					data:"tg="+$(this).attr("name"),
					success:function(data){
						$("#right-content .content").html(data);
					},
					error:function(xhr, textStatus, errorThrown){
						alert(textStatus + errorThrown);
					},
					complete: function(){
						$("#right-content .ajax_loading").hide();
					}
				});
			}
			
		});
		$("#bodier .menu-list li li,#bodier .menu-list li h3").hover(function(){
			$(this).addClass("hoveron");
		},function(){
			$(this).removeClass("hoveron");
		});

		$("#bodier .menu-list li li").click(function(){
			$("#bodier .menu-list h3").removeClass("selected");
			$("#bodier .menu-list li li").removeClass("selected");
			$(this).addClass("selected");
			//fix title
			$("#right-content .headtitle").text($(this).parent().prev().text());
			$("#right-content .subtitle").text($(this).text());
			$("#right-content .separator").text(" | ");
			//Ajax call
			$("#right-content .ajax_loading").show();
			$.ajax({
				url:"/edu_ajax.do",
				dataType: "html",
				data:"tg="+$(this).attr("name"),
				success:function(data){
					$("#right-content .content").html(data);
				},
				error:function(xhr, textStatus, errorThrown){
					alert(textStatus + errorThrown);
				},
				complete: function(){
					$("#right-content .ajax_loading").hide();
				}
			});
		});
	},
	getParameter: function(key){
		var href = window.location.href;
		var start = href.indexOf(key+"=");
		if(-1 == start){
			return null;
		}else{
			start += key.length+1;
			var tmpstr = href.substr(start);
			var end = tmpstr.indexOf("&");
			if(-1 == end){
				return href.substr(start);
			}else{
				return href.substr(start,end);
			}
		}
	},
	setContentTitle: function(){
		var target = self.getParameter("tg");
		if(target != null && $("h3[name='"+target+"'],li[name='"+target+"']").length>0){
			var menu_obj = $("#left-menu h3[name='"+target+"']");
			if(menu_obj.length>0){
				$("#right-content .headtitle").text(menu_obj.text());
				$("#right-content .separator").text("");
				$("#right-content .subtitle").text("");
			}else{
				menu_obj = $("#left-menu li[name='"+target+"']");
				$("#right-content .subtitle").text(menu_obj.text());
				$("#right-content .headtitle").text(menu_obj.parent().prev().text());
				menu_obj.parent().show();
			}
			menu_obj.addClass("selected");
			
		}else{
			var menu_obj = $("#left-menu .menu-list h3").first();
			$("#right-content .headtitle").text(menu_obj.text());
			if(menu_obj.next().length>0){
				menu_obj = menu_obj.next().children().first();
				$("#right-content .subtitle").text(menu_obj.text());
				menu_obj.parent().show();
			}else{
				$("#right-content .separator").text("");
				$("#right-content .subtitle").text("");
				menu_obj.addClass("selected");
			}
			menu_obj.addClass("selected");
		}	
	}
	
}
$(document).ready(function() {
	GD.front.init();
});